<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */

class Customer extends Base2
{
	public function Customer()
	{	
		$this->title = $GLOBALS['lang']->GetLanguage("Customer", "title");
		$this->toolbar = true;
		$this->moduleID = 140;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-customer";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	private function CheckInput()
	{
		include("./_tables.php");
		
		if($_POST['name'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}
		
		$query = "SELECT * FROM ".$tblcustomer." WHERE name='".$_POST['name']."'";
		
		if($GLOBALS['db']->Query($query)->num_rows)
		{
			if($_POST['action'] == "update")
			{
				$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
				if($_POST['id'] == $res->id)
				{
					return true;
				}
			}
			$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
			$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Common", "name"), $string);
			$string = str_replace("{1}", $_POST['name'], $string);

			$this->errorText = $string;
			return false;
		}
		
		return true;
	}
	public function CheckFastEditInput()
	{
		include("./_tables.php");
		
		if($_GET['field'] == "name")
		{
			$query = $GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." WHERE name='".$_GET['value']."'");
			
			if($query->num_rows)
			{
				$res = Fetch($query);
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Common", "name"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=customer');
					//</script>";
					return false;
				}
			}
		}
		return true;
	}
	
	public function Add()
	{		
		
		$smarty = GetSmarty();
		$smarty->assign("editPanel", true);
	 	
		if($this->error)
		{
			$smarty->assign("error",  $this->errorText);
		}
		
		if($_POST['action'] == "add" && !$this->error)
		{
			foreach($_POST as $key => $value)
			{	
				$_POST[$key] = "";
			}
		}


		$smarty->assign("langNameValue",  $_POST['name']);
		$smarty->assign("langStreetValue",  $_POST['street']);
		$smarty->assign("langCityValue",  $_POST['city']);
		$smarty->assign("langTelValue",  $_POST['telephon']);
		$smarty->assign("type", "save");
		$smarty->assign('hiddenFields', array( 
							array('name' => 'action','value' => 'save'),
                            array('name' => 'option','value' => 'member')));
                            
        $smarty->display('base/customer.tpl');  
		/*include("./_tables.php");
		
		$smarty = GetSmarty();
		
		if($this->error)
		{
			$smarty->assign("error",  $this->errorText);
		}


		$smarty->assign("langNameValue",  $_POST['name']);
		$smarty->assign("langStreetValue",  $_POST['street']);
		$smarty->assign("langCityValue",  $_POST['city']);
		$smarty->assign("langTelValue",  $_POST['telephon']);
		
		if($_POST['action'] == "add")
					$type = "save";
				else
					$type = "update";
		
		$smarty->assign("type", $type);
		$smarty->assign('hiddenFields', array( 
							array('name' => 'action','value' => 'save'),
                            array('name' => 'option','value' => 'member')));
	
		$smarty->display('base/customer.tpl');*/
	}
	public function Edit()
	{		
		include("./_tables.php");
		
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." WHERE id=".$_POST['id'].""));

		$smarty = GetSmarty();
		
		if(isset($_POST['username']))
		{
			if($this->error)
			{
				$smarty->assign("error",  $this->errorText);
			}
			$res->name = $_POST['name'];
			$res->street = $_POST['street'];
			$res->city = $_POST['city'];
			$res->telephon = $_POST['telephon'];
		}
		
		

		$smarty->assign("langNameValue", $res->name);
		$smarty->assign("langStreetValue",  $res->street);
		$smarty->assign("langCityValue",  $res->city);
		$smarty->assign("langTelValue",  $res->telephon);
		
		if($_POST['action'] == "add")
					$type = "save";
				else
					$type = "update";
		
		$smarty->assign("type", $type);
		$smarty->assign('hiddenFields', array( 
							array('name' => 'action','value' => 'update'),
                            array('name' => 'option','value' => 'member'),
                            array('name' => 'id','value' => $_POST["id"])));
		
		
		$smarty->display('base/customer.tpl');
		
		
                            
		/*$panel->AddHidden("action", "update");
		$panel->AddHidden("option", "member");
		$panel->AddHidden("id", $_POST['id']);
		
		$panel->Close();*/
	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
					
		if($_POST['action'] == "add" || $_POST['action'] == "copy")
		{
			$this->Add();
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "activate")
		{
			$this->Activate();
		}
		else if($_POST['action'] == "save")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->SaveObject($tblcustomer);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->UpdateObject($tblcustomer);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Delete($tblcustomer);
			$this->Paint();
		}
		else
		{
			$this->Paint();
		}
	}
	
	public function Activate()
	{
		include("./_tables.php");
		
		$query = "SELECT * FROM ".$tblcustomer." WHERE id='".$_POST['id']."'";
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
		
		
		if($res->activated == "1")
		{
			$activate = "0";
		}
		else
		{
			$activate = "1";
		}
		
		$query = "UPDATE ".$tblcustomer." SET activated='".$activate."' WHERE id='".$_POST['id']."'";
		$GLOBALS['db']->Query($query);
		
		$this->Paint();
	}

	public function Paint()
	{
		include("./_tables.php");
		$this->dataTable = $tblcustomer;
		$this->PaintEditPanel();
		
		$this->smarty->assign("editPanel", true);
	 	
		
		if($this->error)
		{
			$this->smarty->assign("error",  $this->errorText);
		}
		
		if($this->hasData)
		{
			$this->smarty->assign("langNameValue",  $this->data->name);
			$this->smarty->assign("langStreetValue",  $this->data->street);
			$this->smarty->assign("langCityValue",  $this->data->city);
			$this->smarty->assign("langTelValue",  $this->data->telephon);	
		}
		
			
		$type = ($_POST['id'] != "") ? "update" : "save";
		$this->smarty->assign("type", $type);	
		
		$this->smarty->assign('hiddenFields', array( 
					array('name' => 'action','value' => $type),
                    array('name' => 'option','value' => 'member'),
                    array('name' => 'id','value' => $this->data->id),
                    array('name' => 'search','value' => ($_POST ['search'] != "") ? $_POST['search'] : $this->search)));
		
		$this->smarty->display('base/customer.tpl');
		
		/*include("./_tables.php");
		
		echo 
		'<div id="cpanel">';
				
		$dgv = new DataGridView($this);
		$dgv->AddTextFilter($this->filter);
		
		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "name"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "street"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "city"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "telephone"), "25%", true);

		$dgv->CloseHeader();
		
		if($this->filter != "")
		{	
			$query = "SELECT * FROM ".$tblcustomer." WHERE name LIKE '%".$this->filter."%'";
			$res = $GLOBALS['db']->Query($query);
		}
		else
		{
			$res = $GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer);
		}
		
		if($this->permission->editPermission)
			$title = "fastEdit";
		else
			$title = "";
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->name, $title, "", "name");
			$dgv->AddData($line->street, $title, "", "street");
			$dgv->AddData($line->city, $title, "", "city");
			$dgv->AddData($line->telephon, $title, "", "telephon");
			
			$dgv->CloseRow();
		}
		
		$dgv->CloseDataGrid();
		
		echo '</div>';*/
	}
	public function Toolbar()
	{
		if(!$this->allowed)
			return;
			
		PaintDefaultToolbar($this);
	}
	
	public function AddCSS()
	{
			
	}
	public function AddJS()
	{
		
	}
}

?>